import json
from sys import path as path2

from rich import print as rprint
from py3langid.langid import LanguageIdentifier, MODEL_FILE

path2.append('c:/users/tian/desktop')
from Service import service


with open('d:/data/list.json', 'r', encoding='utf-8') as f:
    keywords = json.loads(f.read())

identifier = LanguageIdentifier.from_pickled_model(MODEL_FILE, norm_probs=True)
identifier.set_languages(['zh', 'en', 'ja'])

def aboutTitle(dbFile, title, i, site, complexity):
    '''
    1. 由于部分主播资源的质量也很高，所以将白名单的优先级设为最高
    2. 查重的依据本应该是 magnet ，但一般获取 magnet 需要多请求一个网页，影响速度，故将 title 作为依据
    3. “必看”资源为了追求高画质、无水印的目标，该 tag 下的视频 title 可重复记录，在后面的代码中再根据 magnet 查重
    '''
    whitelist = keywords['whitelist']
    blacklist = keywords['blacklist']
    secondary = keywords['secondary']
    anchorman = keywords['anchorman']
    toupai = keywords['toupai']
    others = keywords['others']
    for k in whitelist:
        if k in title:
            tag = '必看'
            return tag
    if complexity == 1:         # complexity 和是否需要语言识别没有关系，只是碰巧现在的一级页面才需要识别语言
        lang = identifier.classify(title)[0]
        if lang == 'ja':
            print(f'日语，略过 ({i})——{site}')
            return
        elif lang == 'en':
            if service.query('select * from western where title=?', (title,)) != []:
                rprint(f'[green]【{title}】已记录 ({i})——{site}')
                return
            return 'western'
    for k in blacklist:
        if k in title:
            print(f'含过滤词 {k}，略过 ({i})——{site}')
            return
    service.path = dbFile
    if service.query('select * from guochan where title=?', (title,)) != []:    # 判断存在该条记录
        rprint(f'[green]【{title}】已记录 ({i})——{site}')
        return
    for k in anchorman:
        if k in title:
            tag = '主播'
            return tag
    for k in toupai:
        if k in title:
            tag = '偷拍'
            return tag
    for k in others:
        if k in title:
            tag = k
            return tag
    for k in secondary:
        if k in title:
            tag = '探索'
            return tag
    tag = ' '
    return tag
